<?php 
/*
# ------------------------------------------------------------------------
# JA Seleni for Joomla 1.5.x - Version 1.0 - Licence Owner JA115884
# ------------------------------------------------------------------------
# Copyright (C) 2004-2009 J.O.O.M Solutions Co., Ltd. All Rights Reserved.
# @license - Copyrighted Commercial Software
# Author: J.O.O.M Solutions Co., Ltd
# Websites:  http://www.joomlart.com -  http://www.joomlancers.com
# This file may not be redistributed in whole or significant part.
# ------------------------------------------------------------------------
*/
/**
 * JA News Sticker module allows display of article's title from sections or categories. \
 * You can configure the setttings in the right pane. Mutiple options for animations are also added, choose any one.
 * If you are using this module on Teline III template, * then the default module position is "headlines".
 **/
  // no direct access
defined('_JEXEC') or die('Restricted access');

require_once (JPATH_SITE . '/components/com_content/helpers/route.php');
/**
 * modjanewstickerHelper class.
 */
class modjanewstickerHelper {
	
	/**
	 * get listing items from rss link or from list of categories.
	 *
	 * @param JParameter $params
	 * @return array
	 */
	function getList( $params ){
		$rows = array();
		if( strtolower($params->get('using_mode')) != 'rss' ) {
			// check cache was endable ?
			if ( $params->get('enable_cache') ) {
				$cache =& JFactory::getCache();
				$cache->setCaching( true );
				$cache->setLifeTime( $params->get( 'cache_time', 30 ) * 60 );	
				$rows = $cache->get( array( (new modjanewstickerHelper() ) , 'getArticles' ), array( $params ) ); 
			} else {
				$rows = modjanewstickerHelper::getArticles( $params );
			}					
		} else {
			$rows  = modjanewstickerHelper::dataFromRSS( $params );
			
		}
		return $rows;
	}
	
	/**
	 * get articles from list of categories and follow up owner paramer.
	 *
	 * @param JParameter $params.
	 * @return array list of articles
	 */
	function getArticles( $params ){
		$obj = JAStArticles::getInstance();
		$obj->setOrder($params->get('sort_order_field' ,'created'), $params->get('sort_order','DESC'));
		$obj->setLimit( $params->get('max_items', 5) );
		$rows = $obj->getListArticles( $params );
	
		return $rows;							
	}
	
	/**
	 * get  list of items from rss list and process caching.
	 *
	 * @param JParameter $params.
	 * @return array list of articles
	 */
	function dataFromRSS( $params ){
		$data = array();
		if( trim($params->get('rss_link')) == '' ) {
			return $data;
		}
		$rssUrl =  $params->get('rss_link');

		//  get RSS parsed object
		$options = array();
		$options['rssUrl'] 		= $rssUrl;
		if ( $params->get('enable_cache') ) {
			$options['cache_time']  = $params->get( 'cache_time' , '30' ) ;
			$options['cache_time']	*= 60;
		} else {
			$options['cache_time'] = null;
		}

		$rssDoc =& JFactory::getXMLparser( 'RSS', $options );

		if ($rssDoc != false) {
			$items = $rssDoc->get_items();
			if( $items != null ){
				$tmp = array();
				foreach( $items as $item ){ 
					$obj = new stdClass();
					$obj->title = $item->get_title();
					$obj->link = $item->get_link();
					$obj->introtext = $item->get_description();
					$tmp[] = $obj;
				}
				$data = $tmp;
			}
		}
		return $data;	
	}
	
	/**
	 * trim string with max specify
	 *  
	 * @param string $title
	 * @param integer $max.
	 */
	function trimString( $title, $max=60  ){
		if( strlen($title) > $max ){
			return substr( $title, 0, $max ) . '...';
		} 
		return $title;
	}
	
	/**
	 * get description of item: trim string with max chars.
	 *
	 * @param string $introtext
	 * @param string $separator
	 * @param string $descMaxChars
	 * @return string
	 */
	function getDescription( $introtext, $separator, $descMaxChars=60 ){
		return $separator . ' ' . modJAHeadLinesHelper::trimString( strip_tags($introtext), $descMaxChars );
	}
	
	/**
	 * detect and get link with each resource
	 *
	 * @param string $item
	 * @param bool $useRSS.
	 * @return string.
	 */
	function getLink( $item, $useRSS=false ){
		if($useRSS){			
			return $item->link;
		} else {
			return  JRoute::_(ContentHelperRoute::getArticleRoute($item->slug, $item->catslug, $item->sectionid));
		}
	}
	
	/**
	 * get size follow up animation type.
	 *
	 * @param JPramater $params
	 * @return integer.
	 */
	function getSize( $params ) {
		$mode = $params->get('animation_type');
		
		if( $mode == 'vertical'){
			return $params->get('height', 30);
		}	
		return $params->get('width', 500);
	}
}
?>